﻿namespace Softserve.Pottery.Infrastructure
{
    using System.Collections.Generic;
    using System.ServiceModel.Channels;

    /// <summary>
    /// Class who send message from one scope for all another's
    /// </summary>
    public static class Messenger
    {
        public delegate void MessadgeHandler(MyMessage msgForCaller);

        /// <summary>
        /// STATIC 
        /// </summary>
        /// <param name="mes"></param>
        public static void Add(MyMessage mes)
        {
            if (mes != null)
            {
                lock (__mutex)
                {
                    if (ListOfHandlers != null)
                    {
                        ListOfHandlers(mes);
                    }
                }
            }
        }

        /// <summary>
        /// Registration scope method as reviser of message
        /// </summary>
        /// <param name="methodCall"></param>
        public static void Register(MessadgeHandler methodCall)
        {
            ListOfHandlers += methodCall;
        }

        #region Private       

        private static MessadgeHandler ListOfHandlers;

        private static object __mutex = new object();
        
        #endregion
    }
}
